<?php
namespace Validator;

use Hoa\Registry\Registry;
use Library\Arr;
use Respect\Validation\Validator;
use Respect\Validation\Exceptions\NestedValidationException;


/**
 * CnfValidator
 * @author los_gsy
 */
class CnfValidator {
	/**
	 * 验证类型
	 * @param string $type
	 * @return mixed true：表示验证通过；array：表示验证失败，该返回数组即为错误信息；
	 */
	static public function validateType($type) {
		$conf = Registry::get('conf');
		$typeValidator = Validator::notEmpty()->alnum('_')->length(null, 64);
		try {
			$typeValidator->assert($type);
		} catch (NestedValidationException $e) {
			$eMsgs = $e->findMessages([
					'notEmpty' => 410101,
					'alnum' => 410102,
					'length' => 410103,
			]);
			$status = Arr::firstNotEmpty($eMsgs);
			if ($status) {
				$ret = $conf['status'][$status];
				return $ret;
			}
		}
		return true;
	}


	/**
	 * 验证键
	 * @param string $k
	 * @return mixed true：表示验证通过；array：表示验证失败，该返回数组即为错误信息；
	 */
	static public function validateK($k) {
		$conf = Registry::get('conf');
		$kValidator = Validator::notEmpty()->alnum('_')->length(null, 64);
		try {
			$kValidator->assert($k);
		} catch (NestedValidationException $e) {
			$eMsgs = $e->findMessages([
					'notEmpty' => 410201,
					'alnum' => 410202,
					'length' => 410203,
			]);
			$status = Arr::firstNotEmpty($eMsgs);
			if ($status) {
				$ret = $conf['status'][$status];
				return $ret;
			}
		}
		return true;
	}


}